package com.foreknow.web;


import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.Rule;
import com.foreknow.entity.User;
import com.foreknow.service.RuleService;
import com.foreknow.service.UserService;
import com.foreknow.utils.Constance;
import com.foreknow.utils.TokenUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//排班规则
@RestController
@RequestMapping("/rule")
public class RuleController {

    @Autowired
    private RuleService ruleService;

    //展示规则名称列表 参数 规则名称(rulename) 页数pnum 数据条数psize 科室(deptid)
    @ApiOperation("展示规则名称列表")
    @RequestMapping(value = "/findAllByConditionByPage",method = RequestMethod.POST)
    public String findAllByConditionByPage(@RequestBody Map<String,Object> para) throws JsonProcessingException {

        Rule rule = new Rule();
        if(para.get("rulename")!=null){rule.setRulename((String)para.get("rulename"));}
        if (para.get("deptid")!=null){rule.setDeptid((Integer)para.get("deptid"));}

        Integer pageNum = (Integer)para.get("pageNum");
        Integer pageSize =  (Integer)para.get("pageSize");

        PageHelper.startPage(pageNum,pageSize);
        List<Rule> rules = ruleService.selectAllRulesByCondition(rule);
        PageInfo<Rule> pageInfo = new PageInfo<Rule>(rules);
        HashMap<String,Object> hs=new HashMap<>();
        ObjectMapper objectMapper=new ObjectMapper();

        hs.put("meta",new Constance("msg",200));
        hs.put("info",pageInfo);

        return objectMapper.writeValueAsString(hs);
    }
    //根据信息查询排班规则明细 参数 规则名称rulename  页数pnum 数据条数psize 科室deptid
    @ApiOperation("根据信息查询排班规则明细")
    @RequestMapping(value = "/findRuleByCondition",method = RequestMethod.POST)
    public String findRuleByCondition(@RequestBody Map<String,Object> para) throws JsonProcessingException {
        HashMap<String,Object> hs=new HashMap<>();
        ObjectMapper objectMapper=new ObjectMapper();

        Rule rule = new Rule();
        if (para.get("rulename")==null||para.get("deptid")==null){
            //失败
            hs.put("meta",new Constance("msg",404));
            return objectMapper.writeValueAsString(hs);
        }

        rule.setRulename((String)para.get("rulename"));
        rule.setDeptid((Integer)para.get("deptid"));
        Integer pageNum = (Integer)para.get("pageNum");
        Integer pageSize =  (Integer)para.get("pageSize");

        PageHelper.startPage(pageNum,pageSize);
        List<Rule> rules = ruleService.selectByRulenameDeptId(rule);
        PageInfo<Rule> pageInfo = new PageInfo<Rule>(rules);

        hs.put("meta",new Constance("msg",200));
        hs.put("info",pageInfo);

        return objectMapper.writeValueAsString(hs);
    }



    //删除排序规则  参数 规则名称rulename 科室deptid
    @ApiOperation("删除排序规则")
    @RequestMapping(value = "/deleteRuleByRulenameByDeptid",method = RequestMethod.POST)
    public String deleteRuleByRulenameByDeptid(@RequestBody Map<String,Object> para) throws JsonProcessingException {

        Rule rule = new Rule();
        if(para.get("rulename")!=null){rule.setRulename((String)para.get("rulename"));}
        if (para.get("deptid")!=null){rule.setDeptid((Integer)para.get("deptid"));}
        int i = ruleService.deleteByRulenameByDeptIDByLogic(rule);
        HashMap<String,Object> hs=new HashMap<>();
        ObjectMapper objectMapper=new ObjectMapper();
        //判断是否成功
        if (i>0){

            hs.put("meta",new Constance("msg",200));

            return objectMapper.writeValueAsString(hs);
        }
        //失败
        hs.put("meta",new Constance("msg",404));
        return objectMapper.writeValueAsString(hs);
    }

    //根据排班规则名称删除排序规则  参数 规则名称rulename
    @ApiOperation("根据排班规则名称删除排序规则")
    @RequestMapping(value = "/deleteRuleByRulename",method = RequestMethod.POST)
    public String deleteRuleByRulename(@RequestBody Map<String,Object> para) throws JsonProcessingException {

        String rulename = (String) para.get("rulename");
        HashMap<String,Object> hs=new HashMap<>();
        ObjectMapper objectMapper=new ObjectMapper();
        int i = ruleService.deleteByRulenameByLogic(rulename);
        //判断是否成功
        if (i>0){
            hs.put("meta",new Constance("msg",200));
            return objectMapper.writeValueAsString(hs);
        }
        //失败
        hs.put("meta",new Constance("msg",404));
        return objectMapper.writeValueAsString(hs);
    }

    //批量删除排班规则  参数 List<Rule>对象集合 (规则名称rulename 科室deptid)
    @ApiOperation("批量删除排班规则")
    @RequestMapping(value = "/deleteRulesBatch",method = RequestMethod.POST)
    public String deleteRulesBatch(@RequestBody Map<String,Object> para) throws JsonProcessingException {

        HashMap<String,Object> hs=new HashMap<>();
        ObjectMapper objectMapper=new ObjectMapper();
        if (para.get("ruleArray")==null){
            //失败
            hs.put("meta",new Constance("msg",404));
            return objectMapper.writeValueAsString(hs);
        }
        List<Rule> list = (ArrayList<Rule>) para.get("ruleArray");
        int i = ruleService.deleteByRulenameByDeptIDByLogicBatch(list);

        //判断是否成功
        if (i>0){

            hs.put("meta",new Constance("msg",200));

            return objectMapper.writeValueAsString(hs);
        }
        //失败
        hs.put("meta",new Constance("msg",404));
        return objectMapper.writeValueAsString(hs);
    }

    //添加排序规则  参数 List<rule>对象集合
    @ApiOperation("添加排序规则")
    @RequestMapping(value = "/saveRuleList",method = RequestMethod.POST)
    public String saveRuleList(@RequestBody Map<String,Object> para) throws JsonProcessingException {

        List<User> userList = (List<User>) para.get("userList");
        //由LinkedHashMap 转为 entity
        List<User> users = JSON.parseArray(JSON.toJSONString(userList), User.class);
        String rulename = (String)para.get("rulename");


        HashMap<String,Object> hs=new HashMap<>();
        ObjectMapper objectMapper=new ObjectMapper();

        int insert = ruleService.insert(users, rulename);

        //判断是否成功
        if (insert>0){

            hs.put("meta",new Constance("msg",200));
            return objectMapper.writeValueAsString(hs);
        }
        //失败
        hs.put("meta",new Constance("msg",404));
        return objectMapper.writeValueAsString(hs);
    }

    //删除排序规则时对规则进行验证 参数 规则名称rulename 科室deptid
    @ApiOperation("添加排序规则")
    @RequestMapping(value = "/findValidRulenameDeptid",method = RequestMethod.POST)
    public String findValidRulenameDeptid(@RequestBody Map<String,Object> para) throws JsonProcessingException {

        Rule rule = new Rule();
        if(para.get("rulename")!=null){rule.setRulename((String)para.get("rulename"));}
        if (para.get("deptid")!=null){rule.setDeptid((Integer)para.get("deptid"));}
        List<Rule> rules = ruleService.selectByRulenameDeptId(rule);
        HashMap<String,Object> hs=new HashMap<>();
        ObjectMapper objectMapper=new ObjectMapper();

        // 删除 判断有数据
        if (rules.size()>0 ){
            hs.put("meta",new Constance("msg",200));

            return objectMapper.writeValueAsString(hs);
        }

        //获取失败
        hs.put("meta",new Constance("msg",404));

        return objectMapper.writeValueAsString(hs);
    }
    //添加排序规则时对规则进行验证 参数 规则名称rulename 科室deptid
    @ApiOperation("添加排序规则时对规则进行验证")
    @RequestMapping(value = "/findsaveValidRulenameDeptid",method = RequestMethod.POST)
    public String findsaveValidRulenameDeptid(@RequestBody Map<String,Object> para) throws JsonProcessingException {

        Rule rule = new Rule();
        if(para.get("rulename")!=null){rule.setRulename((String)para.get("rulename"));}
        if (para.get("deptid")!=null){rule.setDeptid((Integer)para.get("deptid"));}
        List<Rule> rules = ruleService.selectByRulenameDeptId(rule);
        HashMap<String,Object> hs=new HashMap<>();
        ObjectMapper objectMapper=new ObjectMapper();
        //添加 判断没有数据
        if (rules.size()==0){
            hs.put("meta",new Constance("msg",200));

            return objectMapper.writeValueAsString(hs);
        }
        //获取失败
        hs.put("meta",new Constance("msg",404));

        return objectMapper.writeValueAsString(hs);
    }



}
